package com.example.vp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.RedisConstant;
import com.common.exception.CommonException;
import com.common.holder.UserHolder;
import com.common.utils.IDUtils;
import com.common.utils.RedisUtils;
import com.example.vp.domian.enetity.Subscribe;
import com.example.vp.domian.enetity.Type;
import com.example.vp.domian.vo.ModelVO;
import com.example.vp.mapper.SubscribeMapper;
import com.example.vp.service.SubscribeService;
import com.example.vp.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
public class SubscribeServiceImpl extends ServiceImpl<SubscribeMapper, Subscribe> implements SubscribeService {
    @Autowired
    private TypeService typeService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IDUtils idUtils;
    @Override
    public void subscribe(Set<Long> typeId) {
        Long userId = UserHolder.getUserId();
        //查找是否有对应分类
        List<Type> types = typeService.listByIds(typeId);
        if(ObjectUtils.isEmpty(types)){
            throw new CommonException("分类不存在",401);
        }
        //校验types中是否有list没有的元素

        if(typeId.size()!=types.size()){
            throw new CommonException("未知分类",401);
        }
        //用户订阅分类
        //先将用户订阅信息都删除
        remove(new LambdaQueryWrapper<Subscribe>().eq(Subscribe::getUserId, userId));
        //再将新的订阅信息保存
        saveBatch(typeId.stream().map(item->new Subscribe(idUtils.getId(),item,userId)).toList());
        
        //初始化模型
        ModelVO modelVO = new ModelVO();
        modelVO.setUserId(userId);
        List<String> labels = new ArrayList<>();
        for (Type type : types) {
            labels.addAll(type.getlabelNames());
        }
        System.out.println("好多好多标签：：："+labels);
        modelVO.setLabels(labels);
        initModel(modelVO);
    }

    private void initModel(ModelVO modelVO) {

        String key = RedisConstant.USER_MODEL+modelVO.getUserId();
        Map<Object,Object> map = new HashMap<>();
        if(!ObjectUtils.isEmpty(modelVO.getLabels())){
            int size = modelVO.getLabels().size();
            double probability = 100/size;
            for(String label:modelVO.getLabels()){
                map.put(label,probability);
            }
        }
        redisUtils.remove(key);
        redisUtils.hset(key,map);
    }

    @Override
    public List<Type> listAll() {
        return typeService.list(new LambdaQueryWrapper<Type>().select(Type::getIcon, Type::getName, Type::getId));
    }

    @Override
    public List<Type> getSubscribe(Long userId) {
        List<Subscribe> list = list(new LambdaQueryWrapper<Subscribe>().eq(Subscribe::getUserId, userId));
        Set<Long> ids = new HashSet<>();
        list.forEach(item->{
            ids.add(item.getTypeId());
        });
        return typeService.listByIds(ids);
    }
}
